package cn.ibizlab.core.open.client;

import cn.ibizlab.api.dto.AuthLoginResponseDTO;
import cn.ibizlab.util.security.AuthenticationUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

@FeignClient(value = "${ibiz.ref.service.ebsx:ibizcloud-open}",contextId = "ebsx",url = "${ibiz.ref.service.rt.ebsx.url:}")
public interface EbsxFeignClient {



    @PostMapping(value = "/v7/login")
    AuthLoginResponseDTO ebsxLogin(@Validated @RequestBody Map login) ;

    default AuthLoginResponseDTO ebsxLogin(String clientId, String clientSecret) {
        Map<String,String> login = new LinkedHashMap<>();
        login.put("loginname",clientId);
        login.put("password", clientSecret);
        return ebsxLogin(login);
    }

    @GetMapping(value = "/appdata")
    Map getAppData();

    @GetMapping(value = "/appdata")
    Map getAppData(@RequestHeader(value="srfsystemid") String srfsystemid, @RequestHeader(value="srforgid") String srforgid,  @RequestHeader(value="Authorization") String strToken, @RequestHeader("X-Real-IP") String clientIp);

    @RequestMapping(method = RequestMethod.POST, value = "/open/{id}/syncorganizationunits")
    void syncOrganizationUnits(@PathVariable("id") String id, @RequestBody(required=false) Map body);

    @RequestMapping(method = RequestMethod.POST, value = "/open/{id}/syncorganizationunits")
    void syncOrganizationUnits(@PathVariable("id") String id, @RequestBody(required=false) Map body,@RequestHeader(value="srfsystemid") String srfsystemid,  @RequestHeader(value="srfdcsystemid") String srfdcsystemid, @RequestHeader(value="srforgid") String srforgid, @RequestHeader(value="srfdcid") String srfdcid,  @RequestHeader(value="srfuserid") String srfuserid, @RequestHeader(value="Authorization") String strToken);

    default void syncOrganizationUnits(String id, String defaultDc, Map body, String clientId, String clientSecret) {

        AuthLoginResponseDTO dto = ebsxLogin(clientId, clientSecret);
        AuthenticationUser user = AuthenticationUser.getAuthenticationUser();
        syncOrganizationUnits(id, body, user.getSystemId(), user.getDeploySystemId(), user.getOrganizationId(), user.getDc() == null ? defaultDc: user.getDc(), user.getId(),"Bearer "+dto.getToken());
    }
}